package cz.hanis.android.geography;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cz.hanis.android.geography.models.Continent;
import cz.hanis.android.geography.models.Country;



public class CountryTools {
	
	private Random mRandom = new Random();	
	private List<Country> mCountries;
	private List<Country> mTmpCountryForList;
	
	public CountryTools(List<Country> countries) {
		this.mCountries = countries;
	}
	
	
	public List<Country> getCountries(String continent) {
		if(continent.equals(Continent.ALL)) {
			return new ArrayList<Country>(mCountries);
		} else {
			List<Country> selectedCountries = new ArrayList<Country>(); 
			for (Country country : mCountries) {
				//System.out.println(country);
				//System.out.println(country.getName());
				//System.out.println(country.getContinent());				
				if (country.getContinentId().equals(continent)) {
					selectedCountries.add(country);
				}
			}
			return selectedCountries;
		}		
	}	
	
	
    public List<Country> getRandomCountries(int number, String continent) {
    	List<Country> countriesInContinent = getCountries(continent);
    	List<Integer> list = new ArrayList<Integer>();    	
        list.add(mRandom.nextInt(countriesInContinent.size()));
        while(list.size() < number) {
        	int num = mRandom.nextInt(countriesInContinent.size());
        	if(!list.contains(num)) {
        		list.add(num);
        	}
        }
        List<Country> randomCountries = new ArrayList<Country>();
        for(int i = 0; i < number; i++){
        	randomCountries.add(countriesInContinent.get(list.get(i)));
        }
        return randomCountries;                   	
    }
    
    public Country getRandomCountry(String continent) {
    	return getRandomCountries(1, continent).get(0);
    }
    
    public int getRandom(int number) {
    	return mRandom.nextInt(number);
    }
    
    public List<Integer> getRandomList(int number, int count) {
     	List<Integer> list = new ArrayList<Integer>();    	
        list.add(getRandom(number));
        while(list.size() < count) {
        	int num = getRandom(number);
        	if(!list.contains(num)) {
        		list.add(num);
        	}        	
        }    
        return list;
    }
	
	
    public Country getCountryByName(String name) throws CountryNotFoundException {
    	Country match = null;
    	for(Country country: mCountries) {
    		if(country.getName().equals(name)) {
    			match = country;
    			break;
    		}
    	}
    	if(match == null) {
    		throw new CountryNotFoundException("Country " + name + " not found.");
    	}
    	return match;
    }

    public Country getCountryById(String id) throws CountryNotFoundException {
    	Country match = null;
    	for(Country country: mCountries) {
    		if(country.getId().equals(id)) {
    			match = country;
    			break;
    		}
    	}
    	if(match == null) {
    		throw new CountryNotFoundException("Country " + id + " not found.");
    	}
    	return match;
    }
    
    
    public boolean containsCountry(Country country) {
    	return mCountries.contains(country);
    }
    
    public int getCountryIndex(Country country) throws CountryNotFoundException {
    	if(!getCountries(Continent.EUROPE).contains(country)) {
    		throw new CountryNotFoundException("country " + country.getName() + " doesn't exist.");
    	}
    	return getCountries(Continent.EUROPE).indexOf(country);
    }
    
    public int getCountryIndex(String name) throws CountryNotFoundException {
		Country country = getCountryByName(name);
		return getCountryIndex(country);
	}

	public List<Country> getCountriesWithPrefix(String prefix) {
		List<Country> selectedCountries = new ArrayList<Country>();
		for (Country country : mCountries) {
			if (country.getName().toLowerCase()
					.startsWith(prefix.toLowerCase())) {
				selectedCountries.add(country);
			}
		}
		return selectedCountries;
	}
    

	public List<Country> getTmpCountryForList() {
		return mTmpCountryForList;
	}


	public void setTmpCountryForList(List<Country> tmpCountryForList) {
		this.mTmpCountryForList = tmpCountryForList;
	}
    	
    
    
    
    
    
    
    
    
    
    
    
    

	
}
